/**
 * 扫描枪工具类
 */
var ScanCodeUtil={ 	 	
    keys:[], 	 	
    code:null,
    verify:function(key){
        if(  key.keyCode == 13 ){//Enter
            return false;
        }
        if(  key.keyCode == 16 ){//Shift
            return false;
        }
        return true;
    },
    execute:function(key,callback){
        this.keys.push(key);
        var keys=this.keys;
        var endTime=key.timeStamp;
        var code="";
        if( key.keyCode == 13 ){//扫描枪结束时的自动回车 			
            for( var i=keys.length-1;i >= 0;i-- ){
                if( endTime - keys[i].timeStamp < 50 ){ 	 					
                    if( this.verify(keys[i]) ){
                        code = keys[i].key+code;
                    } 	 					
                    endTime=keys[i].timeStamp;
                }else{
                    break;
                } 	
                        
            } 	 		
            this.code=code; 	 			
            if( code ){
                callback(code); 			
            }		
        }
    },
    listener:function(callback){
        //1.模拟聚焦使得页面可以监听到扫描事件 		
        setInterval(function(){
            var $scanCodeInput= $("<input type='text'/>")
             $("body").append($scanCodeInput);
             $scanCodeInput.focus();
             $scanCodeInput.remove();
            
        }, 1000);
        //2.监听扫描事件
        $("body").keydown(function(event){ 			
             ScanCodeUtil.execute(event,callback);	 	 	
         }); 	 		
    }	 
};

